package com.db.stockexchange.demo;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import com.db.stockexchange.network.Client;
import com.db.stockexchange.network.ResponseListener;
import java.awt.Color;

@SuppressWarnings("serial")
public class EchoClientGUI extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JButton btnNewButton;
	private JMenu mnConnection;
	private JTextArea textArea;
	
	private Client client;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EchoClientGUI frame = new EchoClientGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EchoClientGUI() {
		setForeground(Color.LIGHT_GRAY);
		setTitle("Echo Server Test");
		
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		mnConnection = new JMenu("Connection");
		menuBar.add(mnConnection);
		
		JMenuItem mntmConnect = new JMenuItem("Connect");
		mntmConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					client = new Client();
					
					client.addResponseListener(new ResponseListener() {
						
						@Override
						public void responseReceived(byte[] message) {
							
							String response;
							response = String.format("[%s] %s%n",
									new java.util.Date(),
									new String(message));
							
							textArea.append(response);
						}
					});
					
					(new Thread(client)).start();
					
					textField.setEnabled(true);
					btnNewButton.setEnabled(true);
					mnConnection.setEnabled(false);
					textField.requestFocus();
					JOptionPane.showMessageDialog(EchoClientGUI.this, "Connection successful.");
				} catch (IOException ex) {
					JOptionPane.showMessageDialog(EchoClientGUI.this,
							"Failed to connect.",
							"Error",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		mnConnection.add(mntmConnect);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField = new JTextField();
		textField.setEnabled(false);
		textField.setBounds(10, 11, 315, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		
		btnNewButton = new JButton("Send");
		btnNewButton.setEnabled(false);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String message = textField.getText();
				try {
					client.write(message.getBytes());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		btnNewButton.setBounds(335, 10, 89, 23);
		contentPane.add(btnNewButton);
		
		textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setForeground(Color.BLACK);
		textArea.setBackground(Color.LIGHT_GRAY);
		textArea.setBounds(10, 42, 414, 198);
		contentPane.add(textArea);
	}
}
